package com.learn.redisforspringboot.api.controller;

import com.learn.redisforspringboot.app.SecKillService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;
import java.util.UUID;

/**
 * @author PC
 * 测试controller
 */
@RestController("redisTestController.v1")
@RequestMapping("/v1/redis-test")
public class RedisTestController {

    private RedisTemplate<String, Object> redisTemplate;

    private SecKillService secKillService;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate){
        this.redisTemplate = redisTemplate;
    }

    @Autowired
    public void setSecKillService(SecKillService secKillService) {
        this.secKillService = secKillService;
    }

    @GetMapping
    public String testRedis() {
        redisTemplate.opsForValue().set("name", "lucky");
        return Optional.ofNullable(redisTemplate.opsForValue().get("name")).orElse(StringUtils.EMPTY).toString();
    }

    @GetMapping("/kill")
    public Boolean secKillTest(@RequestParam String prodId) {

        return secKillService.doSecKillByScript(UUID.randomUUID().toString(), prodId);
    }
}
